package com.kky.pp.util

import android.app.Activity
import android.graphics.drawable.Drawable
import android.os.Build
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ImageSpan
import androidx.core.content.ContextCompat

class TextUtil {
    fun addIconsSize(mActivity:Activity,text: String, resId: Int,size:Float): SpannableStringBuilder {
        val drawable = ContextCompat.getDrawable(mActivity, resId)
        val spannableStringBuilder = SpannableStringBuilder(text)

        // 设置图标的宽高
        val iconWidth = DensityUtil.dp2px(mActivity,size)
        val iconHeight = DensityUtil.dp2px(mActivity,size)
        if (drawable!= null) {
            drawable.setBounds(0, 0, iconWidth, iconHeight)
            val imageSpan1 = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                ImageSpan(drawable, ImageSpan.ALIGN_CENTER)
            } else {
                ImageSpan(drawable, ImageSpan.ALIGN_BASELINE)
            }
            spannableStringBuilder.append("   ")
            spannableStringBuilder.setSpan(imageSpan1, spannableStringBuilder.length - 1, spannableStringBuilder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
        return spannableStringBuilder
    }
    fun addIconsMember(mActivity:Activity,text: String, icon1: Drawable?): SpannableStringBuilder {
        val spannableStringBuilder = SpannableStringBuilder(text)

        // 设置图标的宽高
        val iconWidth = DensityUtil.dp2px(mActivity,42f)
        val iconHeight = DensityUtil.dp2px(mActivity,24f)
        if (icon1!= null) {
            icon1.setBounds(0, 0, iconWidth, iconHeight)
            val imageSpan1 = ImageSpan(icon1, ImageSpan.ALIGN_BASELINE)
            spannableStringBuilder.append("  ")
            spannableStringBuilder.setSpan(imageSpan1, spannableStringBuilder.length - 1, spannableStringBuilder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
        return spannableStringBuilder
    }

    fun addIconsToText(mActivity:Activity,text: String, icon1: Drawable?, icon2: Drawable): SpannableStringBuilder {
        val spannableStringBuilder = SpannableStringBuilder(text)
        // 设置图标的宽高
        var iconWidth = DensityUtil.dp2px(mActivity,23f)
        val iconHeight = DensityUtil.dp2px(mActivity,23f)
        if (icon1!= null) {
            icon1.setBounds(0, 0, iconWidth, iconHeight)
            val imageSpan1 = ImageSpan(icon1, ImageSpan.ALIGN_BASELINE)
            spannableStringBuilder.append("  ")
            spannableStringBuilder.setSpan(imageSpan1, spannableStringBuilder.length - 1, spannableStringBuilder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
        iconWidth = DensityUtil.dp2px(mActivity,43f)
        icon2.setBounds(0, 0, iconWidth, iconHeight)
        val imageSpan2 = ImageSpan(icon2, ImageSpan.ALIGN_BASELINE)
        spannableStringBuilder.append("  ")
        spannableStringBuilder.setSpan(imageSpan2, spannableStringBuilder.length - 1, spannableStringBuilder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        return spannableStringBuilder
    }


}